package com.example.apidocserver.domain.document.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.apidocserver.domain.document.entity.ApiParam;
import com.example.apidocserver.domain.document.dao.ApiParamDao;
import com.example.apidocserver.domain.document.entity.ApiParamField;
import com.example.apidocserver.domain.document.service.ApiParamService;
import com.example.apidocserver.infrastructure.util.entity.ApiParamDescUtil;
import com.example.apidocserver.infrastructure.util.result.ParamException;
import com.example.apidocserver.interfaces.vo.ApiParamDescVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 接口入参对象(ApiParam)表服务实现类
 *
 * @author makejava
 * @since 2023-03-07 22:37:23
 */
@Service("apiParamService")
@Slf4j
public class ApiParamServiceImpl implements ApiParamService {
    @Resource
    private ApiParamDao apiParamDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApiParam queryById(Integer id) {
        return this.apiParamDao.queryById(id);
    }

    /**
     * 查询所有对象
     *
     * @return 查询结果
     */
    @Override
    public List<ApiParam> queryAll() {
        return this.apiParamDao.queryAll();
    }

    /**
     * 根据对象查询
     *
     * @param apiParam 筛选条件
     * @return 查询结果
     */
    @Override
    public List<ApiParam> queryByEntity(ApiParam apiParam) {
        return this.apiParamDao.queryByEntity(apiParam);
    }

    /**
     * 新增数据
     *
     * @param apiParam 实例对象
     * @return 实例对象
     */
    @Override
    public ApiParam insert(ApiParam apiParam) {
        this.apiParamDao.insert(apiParam);
        return apiParam;
    }

    /**
     * 修改数据
     *
     * @param apiParam 实例对象
     * @return 实例对象
     */
    @Override
    public ApiParam update(ApiParam apiParam) {
        this.apiParamDao.update(apiParam);
        return this.queryById(apiParam.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.apiParamDao.deleteById(id) > 0;
    }

    @Override
    public List<ApiParam> queryByApiId(Integer apiId) {
        return this.apiParamDao.queryByApiId(apiId);
    }

    @Override
    public void insertList(List<ApiParam> apiParamNewList) {
        if (CollectionUtils.isEmpty(apiParamNewList)) {
            return;
        }
        apiParamDao.insertBatch(apiParamNewList);

        // 设置字段的参数id
        for (ApiParam apiParam : apiParamNewList) {
            if (!CollectionUtils.isEmpty(apiParam.getFields())) {
                for (ApiParamField field : apiParam.getFields()) {
                    field.setParamId(apiParam.getId());
                }
            }
        }
    }

    @Override
    public ApiParam upsertParam(ApiParam paramDescVo) {
        if (Objects.isNull(paramDescVo.getId())) {
            insertParamOne(paramDescVo);
        } else {
            // id存在，则为修改
            ApiParam paramDb = apiParamDao.queryById(paramDescVo.getId());
            if (Objects.isNull(paramDb)) {
                throw new ParamException("数据已不存在。");
            }
            if (!paramDb.equalsParam(paramDescVo)) {
                apiParamDao.update(paramDescVo);
            }
        }
        return paramDescVo;
    }

    private void insertParamOne(ApiParam paramDescVo) {
        // id不存在，为新增参数说明
        if (Objects.isNull(paramDescVo.getApiId()) || Objects.isNull(paramDescVo.getParamType())
                || Objects.isNull(paramDescVo.getDataType()) || StringUtils.isEmpty(paramDescVo.getTitle())) {
            throw new ParamException("接口id、参数类型、数据类型、参数标题不能为空。");
        }
        ApiParamDescUtil.standardize(paramDescVo);
        Date now = new Date();

        // 设置排序
        if (Objects.isNull(paramDescVo.getOrderNo())) {
            Integer maxOrder = apiParamDao.findMaxOrder(paramDescVo);
            if (Objects.isNull(maxOrder)) {
                maxOrder = 1;
            }
            paramDescVo.setOrderNo(maxOrder);
        }
        paramDescVo.setCreateTime(now).setUpdateTime(now);

        // 新增参数说明
        apiParamDao.insert(paramDescVo);
    }
}
